<?php include('includes/header.php'); ?>
<script type="text/javascript">
var FUN_NOMBRE = "Nota de Recepci&oacute;n";

$(document).ready(function(){
    $("h1").html(FUN_NOMBRE);
    initControls();
    jQuery("#formID").validationEngine();
});
        
//FUNCIONES
function initControls(){
    $("#flex1").flexigrid({
        url: 'src/ordencompra.php?modo=1&tipoProducto=1',
        dataType: 'json',
        colModel : [
            {display: 'ID', name : 'odc_id', width : 40, sortable : true, align: 'left'},
            {display: 'C&oacute;digo', name : 'odc_codigo', width : 100, sortable : true, align: 'left'},
            {display: 'Proveedor', name : 'pro_razonsocial', width : 550, sortable : true, align: 'left'},
            {display: 'Fecha', name : 'odc_fechacreacion', width : 150, sortable : true, align: 'left'},
            {display: 'Creado por', name : 'usu_nombre', width : 200, sortable : true, align: 'left'}
        ],
        buttons : [
            {name: 'Nota de Recepci&oacute;n', bclass: 'addElement', onpress : doCommand},
            {separator: true}
        ],
        searchitems : [
            {display: 'C&oacute;digo', name : 'odc_codigo', isdefault: true},
            {display: 'Proveedor', name : 'pro_razoncial'},
            {display: 'Fecha', name : 'odc_fechacreacion'},
            {display: 'Creado por', name : 'usu_nombre'}
        ],
        sortname: "odc_id",
        sortorder: "asc",
        usepager: true,
        title: FUN_NOMBRE,
        useRp: true,
        rp: 16,
        showTableToggleBtn: false,
        resizable: false,
        width: "100%",
        height: 400,
        singleSelect: true
    });	

}
function doCommand(com, grid) {
    if (com == 'Nota de Recepci&oacute;n'){
        $('.trSelected', grid).each(function() {
            var id = $(this).attr('id');
            id = id.substring(id.lastIndexOf('row')+3);
            agregar_modificar(8, id);
        });
    }
    
}     
function agregar_modificar(modo, id){  
   var url = "mNotaRecepcion.php?id="+id;      
   
   $("#somediv").load(url).dialog({
        modal:true,
        width: '70%',
        height: '500',
        title: FUN_NOMBRE,
        buttons: {
            "Guardar": function() {
                //if($('#formID').validationEngine('validate')){
                    var cantRInputElements = $('input[name^=cantR]');
                    var cantPInputElements = $('input[name^=cantP]');
                    var idOcInputElements = $('input[name^=ioc]');
                    var mapInputElements  = $('input[name^=map]');
                    var postData = {
                            iocId   : [], 
                            cantR   : [],
                            cantP   : [],
                            mapId   : [],
                            usuId   : USU_SESION
                    };
                    
                    $.each(cantRInputElements, function(index, el) {
                            postData.cantR.push($(el).val());
                    });
                    $.each(cantPInputElements, function(index, el) {
                            postData.cantP.push($(el).val());
                    });
                    $.each(idOcInputElements, function(index, el) {
                            postData.iocId.push($(el).val());
                    });
                    $.each(mapInputElements, function(index, el) {
                            postData.mapId.push($(el).val());
                    });                    
                    //alert(postData.toSource());
                    $.ajax({
                        url: 'src/ordenCompra.php?modo='+modo,
                        type: "POST",
                        data: postData,
                        success: function(data){
                            $("#somediv").dialog('close');
                            initControls();
                            if(data==1){                                        
                                 getMessage ('success', modo, FUN_NOMBRE, "F");                               
                            }else{
                                 getMessage ('error', modo, FUN_NOMBRE, "F");
                            }
                        }
                    });
                //}   

            },
            "Cerrar": function() {
                $('#formID').validationEngine('hide');
                $( this ).dialog( "close" );

            }
        }
   }); 
}
   
    
</script>  
<h1></h1>
<div id="dialog-confirm" title="Confimaci&oacute;n"></div>
<div id="message"></div>
<table id="flex1" style="display:none"></table>
<div id='somediv'></div>
<?php include('includes/footer.php'); ?>
